/*
 * MainClient.java
 *
 * Created on 21 de Outubro de 2007, 16:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Remote;

import Util.Observer;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Ronaldinho
 */
public class MainClient extends Connectable{
    
    private Observer observer;
    private String serverIP;
    
    private DBClient dbClient;
    
    /** Creates a new instance of MainClient */
    public MainClient(String serverIP, Observer observer) throws IOException {
        super(10001, 10000);
        this.observer = observer;
        this.serverIP = serverIP;
    }
    
    public void loggin(String user, String password){
        Vector<Object> info = new Vector<Object>();
        info.addElement("Login");
        info.addElement(user);
        info.addElement(password);
        Message message = new Message(info, false);
        try {
            this.enviarMensagem(this.serverIP,message);            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    protected void conectar() {
        try {            
            this.receberMensagem();
            this.connectionEnded = this.message.end;
            Vector<Object> info = this.message.info;
            if(!this.connectionEnded){                
                                
                if(this.dbClient == null && info.elementAt(0).equals("OK")){
                    this.dbClient = new DBClient(new Integer((String) info.elementAt(1)).intValue(),new Integer((String) info.elementAt(2)).intValue(), this.serverIP, this.observer);
                    Thread t = new Thread(this.dbClient);
                    t.start();
                }
                
                this.observer.observe(info);
            }                        
            
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    public void sair()
    {
        Message m = new Message(null, true);
        try {
            if(this.dbClient != null)
                this.dbClient.enviarMensagem(this.serverIP, m);            
            this.connectionEnded = true;
        } catch (IOException ex) {
            ex.printStackTrace();
        }       
    }
    
    
    public void run() {
        System.out.println("start MainClient");
        while(!this.connectionEnded){
            this.conectar();
        }
        System.out.println("end MainClient");
    }
    
      public static void main(String[] args) {
        try {
            MainClient client = new MainClient("127.0.0.1",null);
            Thread t = new Thread(client);
            t.start();
            client.loggin(null, null);
            while(client.dbClient == null){
                
            }                                
            client.dbClient.acessarBD(null);
            client.sair();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public DBClient getDBClient() {
        return this.dbClient;
    }
}
